package com.mye.prometheusexportdcim.utils;

import io.prometheus.client.Collector;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;

/**
 * @author hch
 * @创建人 hch
 * @创建时间 2021/5/28 16:40
 * @描述 TODO
 * @版本 1.0.0
 */
public class TextFormat {

    public static final String CONTENT_TYPE_004 = "text/plain; version=0.0.4; charset=utf-8";

    public TextFormat() {
    }

    public static Writer write004(Writer writer, List<Collector.MetricFamilySamples> mfs) throws IOException {
        for (Collector.MetricFamilySamples metricFamilySamples : mfs) {
            writer.write("# HELP ");
            writer.write(metricFamilySamples.name);
            writer.write(32);
            writeEscapedHelp(writer, metricFamilySamples.help);
            writer.write(10);
            writer.write("# TYPE ");
            writer.write(metricFamilySamples.name);
            writer.write(32);
            writer.write(typeString(metricFamilySamples.type));
            writer.write(10);

            for (Iterator var3 = metricFamilySamples.samples.iterator(); var3.hasNext(); writer.write(10)) {
                Collector.MetricFamilySamples.Sample sample = (Collector.MetricFamilySamples.Sample) var3.next();
                writer.write(sample.name);
                if (sample.labelNames.size() > 0) {
                    writer.write(123);
                    for (int i = 0; i < sample.labelNames.size(); ++i) {
                        writer.write((String) sample.labelNames.get(i));
                        writer.write("=\"");
                        writeEscapedLabelValue(writer, (String) sample.labelValues.get(i));
                        writer.write("\"");
                        if (i != sample.labelNames.size() - 1) {
                            writer.write(",");
                        }
                    }

                    writer.write(125);
                }

                writer.write(32);
                writer.write(Collector.doubleToGoString(sample.value));
                if (sample.timestampMs != null) {
                    writer.write(32);
                    writer.write(sample.timestampMs.toString());
                }
            }
            writer.write("# EOF");
            writer.write("\r\n");
        }

        return writer;

    }

    private static void writeEscapedHelp(Writer writer, String s) throws IOException {
        if (StringUtils.isBlank(s)){
            return;
        }
        for (int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            switch (c) {
                case '\n':
                    writer.append("\\n");
                    break;
                case '\\':
                    writer.append("\\\\");
                    break;
                default:
                    writer.append(c);
            }
        }

    }

    private static void writeEscapedLabelValue(Writer writer, String s) throws IOException {
        if (StringUtils.isBlank(s)){
            return;
        }
        for (int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            switch (c) {
                case '\n':
                    writer.append("\\n");
                    break;
                case '"':
                    writer.append("\\\"");
                    break;
                case '\\':
                    writer.append("\\\\");
                    break;
                default:
                    writer.append(c);
            }
        }

    }

    private static String typeString(Collector.Type t) {
        switch (t) {
            case GAUGE:
                return "gauge";
            case COUNTER:
                return "counter";
            case SUMMARY:
                return "summary";
            case HISTOGRAM:
                return "histogram";
            default:
                return "untyped";
        }
    }

}
